<?php

/**
 * SESSION类 Yjnic_Session
 *
 * @author prettyyjnic <prettyyjnic@qq.com> 20151027
 */
class Yjnic_Session{

	/**
	 * SESSION配置
	 */
    protected $_prefix = '';

	/**
	 * @param string $session_id session_id
	 * @param string $config['prefix'] 前缀
	 * @param int $config['expire'] expire time
	 * @link http://php.net/manual/zh/book.session.php
	 */
    public function __construct($config = array(), $session_id=NULL) {    	
    	if ($session_id != NULL) {
            session_id($session_id); //session_id 自定义session，用于无法通过cookie传递session_id的情况
    	}
    	if(isset($config['prefix']))          $this->_prefix = $config['prefix'];
    	if(isset($config['name']))            session_name($config['name']);
    	if(isset($config['path']))            session_save_path($config['path']);
    	if(isset($config['domain']))          ini_set('session.cookie_domain', $config['domain']);
    	if(isset($config['expire']))          ini_set('session.gc_maxlifetime', $config['expire']);
    	if(isset($config['use_trans_sid']))   ini_set('session.use_trans_sid', $config['use_trans_sid']?1:0);
    	if(isset($config['use_cookies']))     ini_set('session.use_cookies', $config['use_cookies']?1:0);
    	if(isset($config['cache_limiter']))   session_cache_limiter($config['cache_limiter']);
    	if(isset($config['cache_expire']))    session_cache_expire($config['cache_expire']);
    	session_start();
    }

	/**
	 * 获取SESSION
	 * @param string $name 待获取的SESSION名字，用.分割二维数组
	 * @return string/NULL/array $name为NULL时返回整个$_SESSION，存在时返回SESSION，否则返回NULL
	 */
    public function get($name = NULL) {
        if ($name === NULL) {
            return $_SESSION[$this->_prefix];
        }
        if (strpos($name, '.')) { //二维数组支持
        	$names = explode('.', $name);
        	return isset($_SESSION[$this->_prefix][$names[0]][$names[1]]) ? $_SESSION[$this->_prefix][$names[0]][$names[1]] : NULL;
        }else{
        	return isset($_SESSION[$this->_prefix][$name]) ? $_SESSION[$this->_prefix][$name] : NULL;
        }

    }

	/**
	 * 设置SESSION
	 * @param string $name 待设置的SESSION名字，用.分割二维数组
	 * @param mix $value 
	 * @return void
	 */
    public function set($name, $value=NULL) {
    	if (strpos($name, '.')) { //二维数组支持
        	$names = explode('.', $name);
        	$_SESSION[$this->_prefix][$names[0]][$names[1]] = $value;
        }else{
        	$_SESSION[$this->_prefix][$name] = $value;
        }
    }

	/**
	 * 删除SESSION
	 *
	 * @param strint $name 待删除的SESSION名字，用.分割二维数组
	 * @return void
	 */
    public function delete($name) {
		if (strpos($name, '.')) { //二维数组支持
	    	$names = explode('.', $name);
	    	$_SESSION[$this->_prefix][$names[0]][$names[1]] = NULL;
	    	unset($_SESSION[$this->_prefix][$names[0]][$names[1]]);
	    }else{
	    	$_SESSION[$this->_prefix][$name] = NULL;
	    	unset($_SESSION[$this->_prefix][$name]);
	    }
    }

    /**
	 * 删除所有SESSION
	 */
    public function deleteAll(){
    	$_SESSION[$this->_prefix] = NULL;
    	unset($_SESSION[$this->_prefix]);
    }

}